package com.ztom.offer.v2;

import java.util.List;

/**
 * 反转链表
 * <p>
 * https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/
 *
 * @author ZhangTao
 */
public class Code22ReverseList {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }

        ListNode pre = null;
        ListNode cur = head;
        ListNode next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;

            pre = cur;
            cur = next;
        }

        return pre;
    }
}
